#include <EB0/EB0.h>

namespace EB0
{
namespace States
{
#pragma region Constructor / Destructor
	// Constructor
	Pause::Pause()
	{
		// Null pointers
		mPauseImage = 0;
		mPauseSprite = 0;
		mPauseRenderable = 0;
		mPauseLayer = 0;
	}

	// Destructor
	Pause::~Pause()
	{

	}
#pragma endregion

#pragma region Load / Unload
	// Load
	void Pause::load(Window::RenderWindow* window)
	{
		// Load the image and sprite
		if (!mPauseImage)
			mPauseImage = Managers::ImageManager::getSingletonPtr()->createImage(PAUSE_SCREEN_PATH);

		if (!mPauseSprite)
			mPauseSprite = new sf::Sprite(*mPauseImage);
	}

	// Unload
	void Pause::unload(Window::RenderWindow* window)
	{
		// Destroy pause screen sprite
		if (mPauseSprite)
		{
			delete mPauseSprite;
			mPauseSprite = 0;
		}

		// Unload pause screen image
		if (mPauseImage)
			Managers::ImageManager::getSingletonPtr()->destroyImage(mPauseImage);
	}
#pragma endregion

#pragma region Create / Destroy Scene
	// Create scene
	void Pause::createScene(Window::RenderWindow* window)
	{
		// Create layer for the pause screen
		if (window)
		{
			// Destroy previous layer
			if (mPauseLayer)
				window->destroyLayer(mPauseLayer);

			// Create new layer
			mPauseLayer = window->createLayer();
		}

		// Destroy previous slideshow renderable
		if (mPauseRenderable)
		{
			if (mPauseLayer)
				mPauseLayer->removeRenderable(mPauseRenderable);

			delete mPauseRenderable;
			mPauseRenderable = 0;
		}

		// Append sprite to the layer
		if (mPauseLayer)
		{
			// Create renderable where slides will be shown
			mPauseRenderable = new Graphics::Renderable(mPauseSprite);
			mPauseLayer->attachRenderable(mPauseRenderable);
		}

		// Partially fade the sprite so you can see behind
		if (mPauseSprite)
			mPauseSprite->SetColor(sf::Color(255, 255, 255, 128));
	}

	// Destroy scene
	void Pause::destroyScene(Window::RenderWindow* window)
	{
		// Destroy pause renderable
		if (mPauseRenderable)
		{
			if (mPauseLayer)
				mPauseLayer->removeRenderable(mPauseRenderable);

			delete mPauseRenderable;
			mPauseRenderable = 0;
		}

		// Destroy layer
		if (window && mPauseLayer)
			window->destroyLayer(mPauseLayer);
	}
#pragma endregion

#pragma region Input and Event Handling
	// On Key Release
	void Pause::onKeyRelease(Window::RenderWindow* window, const sf::Event::KeyEvent& key)
	{
		// Handle key release
		switch (key.Code)
		{
			// Step forward in the slideshow
		case sf::Key::P:
			popState();
			break;

		default:
			break;
		}
	}
#pragma endregion
} // namespace States
}